home *** CD-ROM | disk | FTP | other *** search
/ Total Network Tools 2002 / NextStepPublishing-TotalNetworkTools2002-Win95.iso / Archive / Misc Servers / Zope.exe / WIN32PDHUTIL.PY < prev    next >
Encoding:
Python Source  |  1998-07-15  |  4.1 KB  |  115 lines

  1. """Utilities for the win32 Performance Data Helper module
  2.  
  3. Example:
  4.   To get a single bit of data:
  5.   >>> import win32pdhutil
  6.   >>> win32pdhutil.GetPerformanceAttributes("Memory", "Available Bytes")
  7.   6053888
  8.   >>> win32pdhutil.FindPerformanceAttributesByName("python", counter="Virtual Bytes")
  9.   [22278144]
  10.   
  11.   First example returns data which is not associated with any specific instance.
  12.   
  13.   The second example reads data for a specific instance - hence the list return - 
  14.   it would return one result for each instance of Python running.
  15.  
  16.   In general, it can be tricky finding exactly the "name" of the data you wish to query.  
  17.   Although you can use <om win32pdh.EnumObjectItems>(None,None,(eg)"Memory", -1) to do this, 
  18.   the easiest way is often to simply use PerfMon to find out the names.
  19. """
  20.  
  21. import win32pdh, string, win32api
  22.  
  23. error = win32pdh.error
  24.  
  25. def GetPerformanceAttributes(object, counter, instance = None, inum=-1, format = win32pdh.PDH_FMT_LONG, machine=None):
  26.     path = win32pdh.MakeCounterPath( (machine,object,instance, None, inum,counter) )
  27.     hq = win32pdh.OpenQuery()
  28.     try:
  29.         hc = win32pdh.AddCounter(hq, path)
  30.         try:
  31.             win32pdh.CollectQueryData(hq)
  32.             type, val = win32pdh.GetFormattedCounterValue(hc, format)
  33.             return val
  34.         finally:
  35.             win32pdh.RemoveCounter(hc)
  36.     finally:
  37.         win32pdh.CloseQuery(hq)
  38.  
  39. def FindPerformanceAttributesByName(instanceName, object = "Process", counter = "ID Process", format = win32pdh.PDH_FMT_LONG, machine = None):
  40.     """Find peformance attributes by instance name.
  41.     
  42.     Given a process name, return a list with the requested attributes.
  43.     Most useful for returning a tuple of PIDs given a process name.
  44.     """
  45.  
  46.     items, instances = win32pdh.EnumObjectItems(None,None,object, -1)
  47.     # Track multiple instances.
  48.     instance_dict = {}
  49.     for instance in instances:
  50.         try:
  51.             instance_dict[instance] = instance_dict[instance] + 1
  52.         except KeyError:
  53.             instance_dict[instance] = 0
  54.         
  55.     ret = []
  56.     for instance, max_instances in instance_dict.items():
  57.         for inum in xrange(max_instances+1):
  58.             if instance == instanceName:
  59.                 ret.append(GetPerformanceAttributes(object, counter, instance, inum, format, machine))
  60.     return ret
  61.  
  62. def ShowAllProcesses():
  63.     object = "Process"
  64.     items, instances = win32pdh.EnumObjectItems(None,None,object, win32pdh.PERF_DETAIL_WIZARD)
  65.     # Need to track multiple instances of the same name.
  66.     instance_dict = {}
  67.     for instance in instances:
  68.         try:
  69.             instance_dict[instance] = instance_dict[instance] + 1
  70.         except KeyError:
  71.             instance_dict[instance] = 0
  72.         
  73.     # Bit of a hack to get useful info.
  74.     items = ["ID Process"] + items[:5]
  75.     print "Process Name", string.join(items,",")
  76.     for instance, max_instances in instance_dict.items():
  77.         for inum in xrange(max_instances+1):
  78.             hq = win32pdh.OpenQuery()
  79.             hcs = []
  80.             for item in items:
  81.                 path = win32pdh.MakeCounterPath( (None,object,instance, None, inum, item) )
  82.                 hcs.append(win32pdh.AddCounter(hq, path))
  83.             win32pdh.CollectQueryData(hq)
  84.             print "%-15s\t" % (instance[:15]),
  85.             for hc in hcs:
  86.                 type, val = win32pdh.GetFormattedCounterValue(hc, win32pdh.PDH_FMT_LONG)
  87.                 print "%5d" % (val),
  88.                 win32pdh.RemoveCounter(hc)
  89.             print
  90.             win32pdh.CloseQuery(hq)
  91.  
  92. def BrowseCallBack(counter):
  93.     machine, object, instance, parentInstance, index, counterName = \
  94.         win32pdh.ParseCounterPath(counter)
  95.  
  96.     result = GetPerformanceAttributes(object, counterName, instance, index, win32pdh.PDH_FMT_DOUBLE, machine)
  97.     print "Value of '%s' is" % counter, result
  98.     print "Added '%s' on object '%s' (machine %s), instance %s(%d)-parent of %s" % (counterName, object, machine, instance, index, parentInstance)
  99.  
  100. def browse():
  101.     print "Browsing for counters..."
  102.     win32pdh.BrowseCounters(None,0, BrowseCallBack, win32pdh.PERF_DETAIL_WIZARD, "Python Browser")
  103.  
  104. def test():
  105.     print "Virtual Bytes = ", FindPerformanceAttributesByName("python", counter="Virtual Bytes")     
  106.     print "Available Bytes = ", GetPerformanceAttributes("Memory", "Available Bytes")
  107.     print win32pdh.EnumObjectItems(None,None,"Memory", -1)
  108.     
  109.     
  110.     
  111. if __name__=='__main__':
  112. #    ShowAllProcesses()
  113. #    test()
  114.     browse()
  115.